//
//  CouponUseCollectViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/8/5.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "CouponCanUseCell.h"
#import "CouponCantUseCell.h"

#import "CouponUseCollectViewController.h"

@interface CouponUseCollectViewController ()<UITableViewDelegate, UITableViewDataSource>



@property (nonatomic ,strong) NSMutableArray <CommonCouponModel *> *dataArray;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (weak, nonatomic) IBOutlet UIButton *handleBtn;

@property (nonatomic ,strong) UIViewController *vc;

@property (nonatomic, assign) CGFloat   defaultHeight;

@property (nonatomic, assign) CouponUseType   type;

@property (nonatomic, assign) NSInteger   index;

@end

@implementation CouponUseCollectViewController

- (instancetype)initDefaultHeight:(CGFloat)height vc:(UIViewController *)vc dataSource:(NSMutableArray <CommonCouponModel *>*)dataSource type:(CouponUseType)type {
    self.dataArray = @[].mutableCopy;
    if (self = [super init]) {
        self.vc = vc;
        self.dataArray = @[].mutableCopy;
        self.dataArray = dataSource.mutableCopy;
        self.defaultHeight = height;
        self.type = type;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initPop];
    [self initView];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.handleBtn pp_clipCorners:UIRectCornerAllCorners radius:self.handleBtn.frame.size.height/2];
        [self.view pp_clipCorners:UIRectCornerTopLeft|UIRectCornerTopRight radius:10];
    });
}

- (void)initPop {
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.contentSizeInPopup = CGSizeMake(TTScreenWidth, self.defaultHeight);
    self.popupController.navigationBarHidden = YES;
    [self.popupController.backgroundView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundTap)]];
}

- (void)initView {
    
    self.index = -1;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    //银行卡cell
    [_tableView registerNib:[UINib nibWithNibName:@"CouponCanUseCell" bundle:nil] forCellReuseIdentifier:@"CouponCanUseCellID"];
    
    [_tableView registerNib:[UINib nibWithNibName:@"CouponCantUseCell" bundle:nil] forCellReuseIdentifier:@"CouponCantUseCellID"];

    [self.tableView reloadData];
}

-(void)closeBlockView {
    [self backgroundTap];
}

- (void)backgroundTap  {
    [self.popupController dismiss];
}


//提交选中的券，选中就选  没选中传-1
- (IBAction)closeAction:(id)sender {
    
    [self.popupController dismiss];
    if (self.selectIndex) {
        self.selectIndex(self.index, self.type);
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    CouponCanUseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CouponCanUseCellID" forIndexPath:indexPath];
    [cell fillCellWithModel:self.dataArray[indexPath.row]];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    return cell;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 110;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if (!self.dataArray[indexPath.row].calSel) {
        
        for (CommonCouponModel *model in self.dataArray) {
            model.isSel = NO;
        }
        self.index = -1;
        [self.tableView reloadData];

        return;
    } else {
        
        //选中了就取消选中
        if (self.dataArray[indexPath.row].isSel) {
            self.index = -1;
            self.dataArray[indexPath.row].isSel = NO;
            [self.tableView reloadData];
            return;
        }
        
        //未选中，就清空别的状态，选中当前
        for (CommonCouponModel *model in self.dataArray) {
            model.isSel = NO;
        }
        
        self.dataArray[indexPath.row].isSel = YES;
        if (self.dataArray[indexPath.row].isSel) {
            self.index = indexPath.row;
        } else {
            self.index = -1;
        }
        
        [self.tableView reloadData];
    }
}


- (NSMutableArray<CommonCouponModel *> *)dataArray {
    if (!_dataArray) {
        _dataArray = @[].mutableCopy;
    }
    return _dataArray;
}

@end
